package com.jiezhuo.controller.jss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiezhuo.entity.cmeduSchool.JwXqStudent;
import com.jiezhuo.entity.cmeduSchool.TStuJtcy;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.entity.jssgl.JssStuGagzjl;
import com.jiezhuo.entity.jssgl.JssStuJbqk;
import com.jiezhuo.entity.jssgl.JssStuYd;
import com.jiezhuo.entity.vo.FilterVo;
import com.jiezhuo.entity.vo.JssSqVo;
import com.jiezhuo.entity.vo.JssTjVo;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.IJwXqStudentService;
import com.jiezhuo.service.cmeduSchool.ITStuJtcyService;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.service.jss.JssCommonService;
import com.jiezhuo.service.jss.JssStuGagzjlService;
import com.jiezhuo.service.jss.JssStuJbqkService;
import com.jiezhuo.service.jss.JssXtUserlistService;
import com.jiezhuo.utils.ListUtils;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.SecurityUtils;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务控制器
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-13 08:48:15
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "寄宿生成长档案汇总")
@RequestMapping("/jssStuJbqk")
@Transactional
public class JssStuJbqkController {

    @Autowired
    private final JssStuJbqkService jssStuJbqkService;

    @Autowired
    private IJwXqNjXjService jwXqNjXjService;

    @Autowired
    private final IJwXqStudentService jwXqStudentService;

    @Autowired
    private final XxdwService xxdwService;

    @Autowired
    private final XxdwXqService xxdwXqService;

    private final JssCommonService jsscommonService;

    private final JssStuGagzjlService jssStuGagzjlService;

    private final ITStuJtcyService itStuJtcyService;

    @Autowired
    private IJwXqStudentService iJwXqStudentService;

    private final JssXtUserlistService jssXtUserlistService;

    @GetMapping("getJssZSJLEdit")
    @ApiOperation("寄宿生接受指导记录编辑load")
    public R getJssZSJLEdit(@RequestParam String id) {
//        String id = "d1f0d15e884c4cbd8c516ac245bdc5d1";

        String maxXQ = jwXqNjXjService.getMaxXQ();
        QueryWrapper<JssStuGagzjl> jssStuGagzjlQueryWrapper = new QueryWrapper<>();
        jssStuGagzjlQueryWrapper.eq("ID", id);

        JssStuGagzjl stuGagzjlServiceOne = jssStuGagzjlService.getOne(jssStuGagzjlQueryWrapper);
        // 查询获取学生id
        String stuId = stuGagzjlServiceOne.getStuId();
        // 数据
        // 查询 header 信息
        List<Map<String, Object>> zdJlEdit = jwXqStudentService.getZdJlEdit(maxXQ, stuId);

        QueryWrapper<JssStuJbqk> queryWrapperJbqk = new QueryWrapper();
        queryWrapperJbqk.eq("STUID", stuId);

        List<JssStuJbqk> jssStuJbqks = jssStuJbqkService.list(queryWrapperJbqk);
        // 赋值 对象
        for (Map<String, Object> map : zdJlEdit) {
            for (JssStuJbqk jssStuJbqk : jssStuJbqks) {
                if (map.get("STU_ID").toString().equals(jssStuJbqk.getStuid())) {
                    map.put("jssStuJbqk", jssStuJbqk);
                }
            }
        }
        // 数据
        List<Map<String, Object>> jssZdJlEdit = jssStuGagzjlService.getJssZdJlEdit(id);

        QueryWrapper<TStuJtcy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", stuId);
        queryWrapper.orderByAsc("id");
        List<TStuJtcy> tStuJtcyList = itStuJtcyService.list(queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("jssZdJlEdit", jssZdJlEdit);
        map.put("zdJlEdit", zdJlEdit);
        if (tStuJtcyList.size() > 0) {
            map.put("tStuJtcy", tStuJtcyList.get(0));

        }
        return R.ok().data("data", map);
    }

    @GetMapping("getJssCzCountEdit")
    @ApiOperation("寄宿生成长档案汇总load")
    public R getJssCzCountEdit(@RequestParam("stuId") String stuId) {
        String maxXQ = jwXqNjXjService.getMaxXQ();
//        QueryWrapper<JssStuGagzjl> jssStuGagzjlQueryWrapper = new QueryWrapper<>();
//        jssStuGagzjlQueryWrapper.eq("ID", id);
//
//        JssStuGagzjl stuGagzjlServiceOne = jssStuGagzjlService.getOne(jssStuGagzjlQueryWrapper);
        // 查询获取学生id
//        String stuId = "00141783";
        // 数据
        // 查询 header 信息
        List<Map<String, Object>> zdJlEdit = jwXqStudentService.getZdJlEdit(maxXQ, stuId);

        QueryWrapper<JssStuJbqk> queryWrapperJbqk = new QueryWrapper();
        queryWrapperJbqk.eq("STUID", stuId);

        List<JssStuJbqk> jssStuJbqks = jssStuJbqkService.list(queryWrapperJbqk);
        // 赋值 对象
        for (Map<String, Object> map : zdJlEdit) {
            for (JssStuJbqk jssStuJbqk : jssStuJbqks) {
                if (map.get("STU_ID").toString().equals(jssStuJbqk.getStuid())) {
                    map.put("jssStuJbqk", jssStuJbqk);
                }
            }
        }
        QueryWrapper<TStuJtcy> tStuJtcyQueryWrapper = new QueryWrapper<>();
        tStuJtcyQueryWrapper.eq("stu_id", stuId);
        // 数据
        List<TStuJtcy> tStuJtcies = itStuJtcyService.list(tStuJtcyQueryWrapper);
        // 数据
        List<Map<String, Object>> jssCzCountEdit = jssStuGagzjlService.getJssCzCountEdit(stuId);


        Map<String, Object> map = new HashMap<>();
        map.put("zdJlEdit", zdJlEdit);
        map.put("jssStuJbqks", jssStuJbqks);
        map.put("tStuJtcies", tStuJtcies);
        map.put("jssCzCountEdit", jssCzCountEdit);

        return R.ok().data("data", map);
    }

    @GetMapping("getJssCzCount")
    @ApiOperation("寄宿生成长档案汇总查询")
    public R getJssCzCount(@RequestParam Map<String, Object> prop) {
        Boolean isRoleXiao = jssXtUserlistService.isRoleXiao();
        String gzXqdm = jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm();
        if (isRoleXiao) {
            prop.put("xq", gzXqdm);
        }

        prop.put("hz", 1);
        Map<String, Object> mapReturn = new HashMap<>();
        // 学校控件
        Map<String, String> xqMap = new HashMap<>();
        Set<String> xqSet = xqMap.keySet();
        List<FilterVo> xq = new ArrayList<FilterVo>();

        //年级控件
        Map<String, String> njMap = new HashMap<>();
        Set<String> njSet = njMap.keySet();
        List<FilterVo> nj = new ArrayList<FilterVo>();

        // 班级控件
        Map<String, String> bjMap = new HashMap<>();
        Set<String> bjSet = bjMap.keySet();
        List<FilterVo> bj = new ArrayList<FilterVo>();


        List<JssTjVo> jssTjVosMax = getJssTjVos(prop);


        jssTjVosMax.forEach(x -> xqMap.put(x.getNewXqdm(), x.getXqjc()));

        // 初始化学校控件
//        mapReturn.put("xq", xq);
        for (String s : xqSet) {
            FilterVo filterVo = new FilterVo();
            filterVo.setXqDm(s);
            filterVo.setXqJc(xqMap.get(s));
            xq.add(filterVo);
        }

        if (isRoleXiao) {
            xq = xq.stream().filter(x -> x.getXqDm().equals(gzXqdm)).collect(Collectors.toList());
        }


        mapReturn.put("xq", xq);
        //下拉列表
        if (prop.get("xq") != null && !"".equals(prop.get("xq").toString())) {
            String xqDm = prop.get("xq").toString();
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNewXqdm().equals(xqDm)).collect(Collectors.toList());
            // 初始化年级控件
            jssTjVosMax.forEach(x -> njMap.put(x.getNj(), x.getNjmc()));

            for (String s : njSet) {
                FilterVo filterVo = new FilterVo();
                filterVo.setNjDm(s);
                filterVo.setNjJc(njMap.get(s));
                nj.add(filterVo);
            }

            mapReturn.put("nj", nj);
            if (prop.get("nj") != null && !"".equals(prop.get("nj").toString())) {
                jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNj().equals(prop.get("nj").toString())).collect(Collectors.toList());
                // 初始化班级控件
                jssTjVosMax.forEach(x -> bjMap.put(x.getBj(), x.getBj() + "班"));

                for (String s : bjSet) {
                    FilterVo filterVo = new FilterVo();
                    filterVo.setBjDm(s);
                    filterVo.setBjJc(bjMap.get(s));
                    bj.add(filterVo);
                }
                mapReturn.put("bj", bj);
            }
        }
        if (prop.get("xm") != null && !"".equals(prop.get("xm").toString())) {
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getXm().contains(prop.get("xm").toString())).collect(Collectors.toList());
        }
        if (jssTjVosMax.size() != 0) {
            jssTjVosMax = (List<JssTjVo>) ListUtils.subList(jssTjVosMax, Integer.parseInt(prop.get("size").toString()), Integer.parseInt(prop.get("current").toString()));
        }
        mapReturn.put("count", jssTjVosMax.size());
        mapReturn.put("data", jssTjVosMax);

        return R.ok().data("data", mapReturn);

    }

    @GetMapping("getJssQd")
    @ApiOperation("寄宿生清单查询")
    public R getJssQd(@RequestParam Map<String, Object> prop) {
        Map<String, Object> mapReturn = new HashMap<>();
        String gzXqdm = jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm();
        Boolean isRoleXiao = jssXtUserlistService.isRoleXiao();

        if (isRoleXiao) {
            prop.put("xq", gzXqdm);
        }

        // 学校控件
        Map<String, String> xqMap = new HashMap<>();
        Set<String> xqSet = xqMap.keySet();
        List<FilterVo> xq = new ArrayList<FilterVo>();

        //年级控件
        Map<String, String> njMap = new HashMap<>();
        Set<String> njSet = njMap.keySet();
        List<FilterVo> nj = new ArrayList<FilterVo>();

        // 班级控件
        Map<String, String> bjMap = new HashMap<>();
        Set<String> bjSet = bjMap.keySet();
        List<FilterVo> bj = new ArrayList<FilterVo>();

        List<JssTjVo> jssTjVosMax = getJssTjVos(prop);


        jssTjVosMax.forEach(x -> xqMap.put(x.getNewXqdm(), x.getXqjc()));

        // 初始化学校控件
//        mapReturn.put("xq", xq);
        for (String s : xqSet) {
            FilterVo filterVo = new FilterVo();
            filterVo.setXqDm(s);
            filterVo.setXqJc(xqMap.get(s));
            xq.add(filterVo);
        }
        if (isRoleXiao) {
            xq = xq.stream().filter(x -> x.getXqDm().equals(gzXqdm)).collect(Collectors.toList());
        }
        mapReturn.put("xq", xq);
        //下拉列表
        if (prop.get("xq") != null && !"".equals(prop.get("xq").toString())) {
            String xqDm = prop.get("xq").toString();
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNewXqdm().equals(xqDm)).collect(Collectors.toList());
            // 初始化年级控件
            jssTjVosMax.forEach(x -> njMap.put(x.getNj(), x.getNjmc()));

            for (String s : njSet) {
                FilterVo filterVo = new FilterVo();
                filterVo.setNjDm(s);
                filterVo.setNjJc(njMap.get(s));
                nj.add(filterVo);
            }

            mapReturn.put("nj", nj);
            if (prop.get("nj") != null && !"".equals(prop.get("nj").toString())) {
                jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNj().equals(prop.get("nj").toString())).collect(Collectors.toList());
                // 初始化班级控件
                jssTjVosMax.forEach(x -> bjMap.put(x.getBj(), x.getBj() + "班"));

                for (String s : bjSet) {
                    FilterVo filterVo = new FilterVo();
                    filterVo.setBjDm(s);
                    filterVo.setBjJc(bjMap.get(s));
                    bj.add(filterVo);
                }
                mapReturn.put("bj", bj);
            }
        }
        if (prop.get("xm") != null && !"".equals(prop.get("xm").toString())) {
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getXm().contains(prop.get("xm").toString())).collect(Collectors.toList());
        }
        mapReturn.put("count", jssTjVosMax.size());
        if (jssTjVosMax.size() != 0) {
            jssTjVosMax = (List<JssTjVo>) ListUtils.subList(jssTjVosMax, Integer.parseInt(prop.get("size").toString()), Integer.parseInt(prop.get("current").toString()));
        }

        mapReturn.put("data", jssTjVosMax);

        return R.ok().data("data", mapReturn);
    }

    @GetMapping("getJssZSJL")
    @ApiOperation("寄宿生接受指导记录")
    public R getJssZSJL(@RequestParam Map<String, Object> prop) {
        Map<String, Object> mapReturn = new HashMap<>();
        // 学校控件
        Map<String, String> xqMap = new HashMap<>();
        Set<String> xqSet = xqMap.keySet();
        List<FilterVo> xq = new ArrayList<FilterVo>();

        //年级控件
        Map<String, String> njMap = new HashMap<>();
        Set<String> njSet = njMap.keySet();
        List<FilterVo> nj = new ArrayList<FilterVo>();

        // 班级控件
        Map<String, String> bjMap = new HashMap<>();
        Set<String> bjSet = bjMap.keySet();
        List<FilterVo> bj = new ArrayList<FilterVo>();


        List<JssTjVo> jssTjVosMax = getJssTjVos(prop);

        // 获取所有记录
        List<JssStuGagzjl> jssStuGagzjlList = jssStuGagzjlService.list();

        for (JssTjVo tjVosMax : jssTjVosMax) {
            List<JssStuGagzjl> collect = jssStuGagzjlList.stream().filter(x -> tjVosMax.getStuId().equals(x.getStuId())).sorted(Comparator.comparing(JssStuGagzjl::getGadate)).collect(Collectors.toList());
            tjVosMax.getJssStuGagzjlList().addAll(collect);
        }


        jssTjVosMax.forEach(x -> xqMap.put(x.getNewXqdm(), x.getXqjc()));

        // 初始化学校控件
//        mapReturn.put("xq", xq);
        for (String s : xqSet) {
            FilterVo filterVo = new FilterVo();
            filterVo.setXqDm(s);
            filterVo.setXqJc(xqMap.get(s));
            xq.add(filterVo);
        }
        mapReturn.put("xq", xq);
        //下拉列表
        if (prop.get("xq") != null && !"".equals(prop.get("xq").toString())) {
            String xqDm = prop.get("xq").toString();
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNewXqdm().equals(xqDm)).collect(Collectors.toList());
            // 初始化年级控件
            jssTjVosMax.forEach(x -> njMap.put(x.getNj(), x.getNjmc()));

            for (String s : njSet) {
                FilterVo filterVo = new FilterVo();
                filterVo.setNjDm(s);
                filterVo.setNjJc(njMap.get(s));
                nj.add(filterVo);
            }

            mapReturn.put("nj", nj);
            if (prop.get("nj") != null && !"".equals(prop.get("nj").toString())) {
                jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getNj().equals(prop.get("nj").toString())).collect(Collectors.toList());
                // 初始化班级控件
                jssTjVosMax.forEach(x -> bjMap.put(x.getBj(), x.getBj() + "班"));

                for (String s : bjSet) {
                    FilterVo filterVo = new FilterVo();
                    filterVo.setBjDm(s);
                    filterVo.setBjJc(bjMap.get(s));
                    bj.add(filterVo);
                }
                mapReturn.put("bj", bj);
            }
        }
        if (prop.get("xm") != null && !"".equals(prop.get("xm").toString())) {
            jssTjVosMax = jssTjVosMax.stream().filter(x -> x.getXm().contains(prop.get("xm").toString())).collect(Collectors.toList());
        }
        mapReturn.put("count", jssTjVosMax.size());
        if (jssTjVosMax.size() != 0) {
            jssTjVosMax = (List<JssTjVo>) ListUtils.subList(jssTjVosMax, Integer.parseInt(prop.get("size").toString()), Integer.parseInt(prop.get("current").toString()));
        }

        mapReturn.put("data", jssTjVosMax);

        return R.ok().data("data", mapReturn);
    }

    @PostMapping("/updateJssSq")
    @ApiOperation("寄宿生清单修改")
    public R updateJssSq(@RequestBody JssSqVo jssSqVo) {

        List<TStuJtcy> jtcyList = jssSqVo.getJtcyList();
        ///////////////////////////\\\\\\\\\\\\\\\\\\\\
        for (TStuJtcy tStuJtcy : jtcyList) {
            tStuJtcy.setOperator(SecurityUtils.getCurrentUserId());
            tStuJtcy.setOperateTime(new Date());
            if (StringUtils.isStringNotExist(tStuJtcy.getId())) {
                tStuJtcy.setId(UUID.randomUUID().toString());
                if (tStuJtcy.getAge() == null) {
                    tStuJtcy.setAge(0);
                }
                itStuJtcyService.save(tStuJtcy);
            } else {
                UpdateWrapper<TStuJtcy> updateTStuJtcy = new UpdateWrapper<>();
                updateTStuJtcy.eq("ID", tStuJtcy.getId());
                itStuJtcyService.update(tStuJtcy, updateTStuJtcy);
            }
        }

        JssStuYd jssStuYd = jssSqVo.getJssStuYd();

        // 处理学生所在学校
        String maxXQ = jwXqNjXjService.getMaxXQ();
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setBj(jssStuYd.getBj());
        jwXqStudent.setGlfs("2");
        jwXqStudent.setOperator(SecurityUtils.getCurrentUserId());
        jwXqStudent.setOperateTime(new Date());

        UpdateWrapper<JwXqStudent> xqStudentUpdateWrapper = new UpdateWrapper<>();
        xqStudentUpdateWrapper.eq("stu_id", jssStuYd.getStuId());
        xqStudentUpdateWrapper.eq("xq", maxXQ);
        iJwXqStudentService.update(jwXqStudent, xqStudentUpdateWrapper);


        JssStuJbqk jssStuJbqk = new JssStuJbqk();
        jssStuJbqk.setJhrlb(jssStuYd.getJhrlb());
        jssStuJbqk.setJkzk(jssStuYd.getJkzk());
        jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
        jssStuJbqk.setOperateTime(new Date());
        jssStuJbqk.setRemoved("0");

        UpdateWrapper<JssStuJbqk> stuJbqkUpdateWrapper = new UpdateWrapper<>();
        stuJbqkUpdateWrapper.eq("STUID", jssStuYd.getStuId());

        boolean updateJbqk = jssStuJbqkService.update(jssStuJbqk, stuJbqkUpdateWrapper);


        return R.ok().data("data", "操作成功");
    }

    @GetMapping("getStuAndJiaQkFilterByXqdm")
    @ApiOperation("学生与家庭基本情况过滤校区代码")
    public R getStuAndJiaQkFilterByXqdm() {
        QueryWrapper<XxdwXq> xxdwXqQuery = new QueryWrapper<>();
        if (jssXtUserlistService.isRoleXiao()) {
            xxdwXqQuery.eq("NEW_XQDM", jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        }
        xxdwXqQuery.eq("SFJSZGL", "1");
        xxdwXqQuery.eq("DQZT", "1");
        xxdwXqQuery.orderByAsc("XQJC");

        List<XxdwXq> list = xxdwXqService.list(xxdwXqQuery);
        return R.ok().data("data", list);
    }

    @GetMapping("getStuAndJiaQkFilterByNj")
    @ApiOperation("学生与家庭基本情况过滤年级")
    public R getStuAndJiaQkFilterByNj(String xqdm) {
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNewXqdm(xqdm);
        jwXqStudent.setXq(jwXqNjXjService.getMaxXQ());
        List<Map<String, Object>> stuAndJiaQkFilterByNj = jwXqStudentService.getStuAndJiaQkFilterByNj(jwXqStudent);
        return R.ok().data("data", stuAndJiaQkFilterByNj);
    }

    @GetMapping("getStuAndJiaQkFilterByBj")
    @ApiOperation("学生与家庭基本情况过滤班级")
    public R getStuAndJiaQkFilterByBj(String xqdm, String nj) {
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNewXqdm(xqdm);
        jwXqStudent.setNj(nj);
        jwXqStudent.setXq(jwXqNjXjService.getMaxXQ());
        List<Map<String, Object>> stuAndJiaQkFilterByBj = jwXqStudentService.getStuAndJiaQkFilterByBj(jwXqStudent);
        return R.ok().data("data", stuAndJiaQkFilterByBj);
    }

    @GetMapping("getStuAndJiaQk")
    @ApiOperation("获取学生与家庭基本情况")
    public R getStuAndJiaTingQk(JwXqStudent jwXqStudent) {

        HashMap<String, Object> objectHashMap = new HashMap<>();
        // 查询 信息
        jwXqStudent.setXq(jwXqNjXjService.getMaxXQ());
        List<Map<String, Object>> stuAndJiaTingQk = jwXqStudentService.getStuAndJiaTingQk(jwXqStudent);
        Set<String> stuid = stuAndJiaTingQk.stream().map(x -> x.get("STUID").toString()).collect(Collectors.toSet());

        //JSS_STU_JBQK
        QueryWrapper<JssStuJbqk> queryWrapperJbqk = new QueryWrapper();
        queryWrapperJbqk.eq("Removed", "0");
        queryWrapperJbqk.in("STUID", stuid);
        List<JssStuJbqk> jssStuJbqkList = jssStuJbqkService.list(queryWrapperJbqk);

        List<Map<String, Object>> stuAndJiaTingQkNew = new ArrayList<>();

        for (Map<String, Object> map : stuAndJiaTingQk) {
            for (JssStuJbqk jssStuJbqk : jssStuJbqkList) {
                if (jssStuJbqk.getStuid().equalsIgnoreCase(map.get("STUID").toString())) {
                    map.put("jssStuJbqk", jssStuJbqk);
                    stuAndJiaTingQkNew.add(map);
                }
            }
        }

        if (stuAndJiaTingQkNew.size() != 0) {
            objectHashMap.put("count", stuAndJiaTingQkNew.size());
            stuAndJiaTingQkNew = (List<Map<String, Object>>) ListUtils.subList(stuAndJiaTingQkNew, jwXqStudent.getPageSize(), jwXqStudent.getPageNum());
            objectHashMap.put("data", stuAndJiaTingQkNew);
            return R.ok().data("data", objectHashMap);

        } else {
            return R.ok().data("data", null);
        }
    }

    @PostMapping("updateStuAndJiaTingQk")
    @ApiOperation("修改学生家庭情况")
    public R updateStuAndJiaTingQk(@RequestBody JssStuJbqk jssStuJbqk) {
        UpdateWrapper<JssStuJbqk> stuJbqkUpdateWrapper = new UpdateWrapper<>();
        stuJbqkUpdateWrapper.eq("ID", jssStuJbqk.getId());
        boolean updateJbqk = jssStuJbqkService.update(jssStuJbqk, stuJbqkUpdateWrapper);
        return R.ok().data("data", "操作成功");
    }

    @GetMapping("updateStuAndJiaTingQkLoad")
    @ApiOperation("修改学生家庭情况初始化")
    public R updateStuAndJiaTingQkLoad(String stuId) {
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setStuId(stuId);
        jwXqStudent.setXq(jwXqNjXjService.getMaxXQ());

        List<Map<String, Object>> list = jwXqStudentService.updateStuAndJiaTingQkLoad(jwXqStudent);
        Map<String, Object> map = new HashMap<>();
        if (list.size() > 0) {
            map = list.get(0);
            String stu_id = map.get("STU_ID").toString();
            QueryWrapper<JssStuJbqk> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("stuid", stu_id);
            JssStuJbqk one = jssStuJbqkService.getOne(queryWrapper);

            List<Map<String, Object>> dm_jhrlb = jsscommonService.getDMAll("dm_jhrlb");
            Map<String, Object> convertMap = ListUtils.convertMap(dm_jhrlb, "DM", "MC");
            one.setJhrlbString(convertMap.get(one.getJhrlb()) != null ? convertMap.get(one.getJhrlb()).toString() : "");
            map.put("jssStuJbqk", one);
        }

        return R.ok().data("data", map);
    }

    private List<JssTjVo> getJssTjVos(Map<String, Object> prop) {
        List<String> jssStudentIds = new ArrayList<>();

        Map<String, String> yxqDmMap = new HashMap();

        Map<String, String> jhrMap = new HashMap();

        Map<String, String> jkztMap = new HashMap();
// 查询 健康状态代码
        Map<String, String> dm_jkzkMap = new HashMap<>();
        List<Map<String, Object>> dm_jkzk = jsscommonService.getDMAll("dm_jkzk");
        dm_jkzk.forEach(x -> {
            dm_jkzkMap.put(x.get("DM").toString(), x.get("MC").toString());
        });

// 查询 监护人字典
        Map<String, String> dm_jhrlbMap = new HashMap<>();
        List<Map<String, Object>> dm_jhrlb = jsscommonService.getDMAll("dm_jhrlb");

        dm_jhrlb.forEach(x -> {
            dm_jhrlbMap.put(x.get("DM").toString(), x.get("MC").toString());
        });

        // 获取最大的学期编号
        Map<String, Object> map = new HashMap<>();
        String maxXQ = jwXqNjXjService.getMaxXQ();
        map.put("XQ", maxXQ);
        // 是汇总 就传这个参数 其他的不用传
        if (prop.get("hz") != null) {
            map.put("hz", 1);
        }

        // 获取所有寄宿生
        List<JssStuJbqk> jssstuJbqkList = jssStuJbqkService.list();
        jssstuJbqkList.forEach(x -> {
            jssStudentIds.add(x.getStuid());
            yxqDmMap.put(x.getStuid(), x.getYxqdm());
            jhrMap.put(x.getStuid(), x.getJhrlb());
            jkztMap.put(x.getStuid(), x.getJkzk());

        });


        // 获取所有寄宿生详细信息
        List<Map<String, Object>> jssGlAllStudent = jwXqStudentService.getJssGlAllStudent(map);

        List<JssTjVo> jssTjVoList = convertIsStu(jssGlAllStudent);
        List<JssTjVo> jssTjVosMax = jssTjVoList.stream().filter(x -> jssStudentIds.contains(x.getStuId())).collect(Collectors.toList());


        // 赋值 监护人 和 健康状态
        jssTjVosMax = jssTjVosMax.stream().peek(x -> {
            x.setJhrDm(jhrMap.get(x.getStuId()));
            x.setJhrMc(dm_jhrlbMap.get(jhrMap.get(x.getStuId())));
            x.setJkztDm(jkztMap.get(x.getStuId()));
            x.setJkztMc(dm_jkzkMap.get(jkztMap.get(x.getStuId())));

        }).collect(Collectors.toList());


        // 设置年级 和 原来学校dm
        Map<String, String> dmnjMap = new HashMap<>();
        List<Map<String, Object>> dm_nj = xxdwService.getDistList("dm_nj");
        for (Map<String, Object> dmnj : dm_nj) {
            dmnjMap.put(String.valueOf(dmnj.get("DM")), String.valueOf(dmnj.get("MC")));
        }

        //设置学校简称
        List<XxdwXq> xxdwXqList = xxdwXqService.list();
        Map<String, String> xxdmxqMap = new HashMap<>();
        for (XxdwXq xxdwXq : xxdwXqList) {
            xxdmxqMap.put(xxdwXq.getNewXqdm(), xxdwXq.getXqjc());
        }


        for (JssTjVo jssTjVo : jssTjVosMax) {
            // 设置年级代码
            jssTjVo.setNjmc(dmnjMap.get(jssTjVo.getNj()));
            //设置原来学校代码
            jssTjVo.setYnewXqdm(yxqDmMap.get(jssTjVo.getStuId()));
            // 设置学校简称
            jssTjVo.setXqjc(xxdmxqMap.get(jssTjVo.getNewXqdm()));
            jssTjVo.setYxqjc(xxdmxqMap.get(jssTjVo.getYnewXqdm()));

        }
        return jssTjVosMax;
    }

    private List<JssTjVo> convertIsStu(List<Map<String, Object>> list) {
        List<JssTjVo> jssTjVoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JssTjVo jssTjVo = new JssTjVo();
            jssTjVo.setBj(list.get(i).get("BJ").toString());
            jssTjVo.setNewXqdm(list.get(i).get("NEW_XQDM").toString());
            jssTjVo.setNj(list.get(i).get("NJ").toString());
            jssTjVo.setStuId(list.get(i).get("STU_ID").toString());
            jssTjVo.setXbm(list.get(i).get("XBM").toString());
            jssTjVo.setXm(list.get(i).get("XM").toString());
            jssTjVoList.add(jssTjVo);
        }
        return jssTjVoList;
    }
}